package com.joelbitar.switchctrl;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class servers extends ListActivity {

	private static final int MENU_ADD = 0;
	private static final int MENU_UPDATE = 1;
	
	private ServerListAdapter serveradapter;
	private ArrayList<Server> servers = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_servers);
        
       // Setting the serverAdapter
        servers = new ArrayList<Server>();
        
        serveradapter = new ServerListAdapter(this, R.layout.settings_servers_row, servers);
        setListAdapter(serveradapter);
        
        ListView listview = getListView();
        
        // Find the listView and add the setOnItemClickListener on it.
        // listView.setOnItemClickListener(new OnItemClickListener);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> a, View v, int position, long id) {
              Log.i("onitemclick", "On item click");
              
              Server server = (Server) v.getTag();
              
              Log.i("server", server.getName());
              
              startEditActivity(server);
              
           }
        });
        
        
        listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> a, View v, int position, long id) {
               Log.i("onitemclick", "On item click");
               
               Server server = (Server) v.getTag();
               
               Log.i("server", server.getName());
               
               showServerDialog(server);
               
               return true;
            }
         });
        
        updateServerList();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_ADD, 0, getString(R.string.menu_settings_servers_add)).setIcon(android.R.drawable.ic_menu_add);
		menu.add(0, MENU_UPDATE, 0, getString(R.string.menu_settings_servers_update)).setIcon(android.R.drawable.ic_menu_rotate);
		return true;
	}
   
	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case MENU_ADD:
		    	launchAddServerActivity();
		        return true;
		    case MENU_UPDATE:
		    	updateServerList();
		        return true;
	    }
	    return false;
	}
	
	public boolean launchAddServerActivity() {
		
		Intent myIntent = new Intent();
		//myIntent.setClassName("com.android.samples", "com.android.samples.Animation1");
		myIntent.setClassName("com.joelbitar.switchctrl", "com.joelbitar.switchctrl.serverdetails");
		myIntent.putExtra(serverdetails.INTENT_ADD, true);
		startActivity(myIntent);
		
		return true;
	}
	
	public boolean updateServerList() {
        ServerDbAdapter serverdbadapter = new ServerDbAdapter(this);
        
        Cursor allServersCursor = serverdbadapter.getAllServers();
        
        servers.clear();
        
        if(allServersCursor.moveToFirst())
        {
        	do {
        		String name = allServersCursor.getString(ServerDbAdapter.NAME_COLUMN);
        		long id = allServersCursor.getLong(ServerDbAdapter.ID_COLUMN);
        		String url = allServersCursor.getString(ServerDbAdapter.URL_COLUMN);
        		String username = allServersCursor.getString(ServerDbAdapter.USERNAME_COLUMN);
        		String password = allServersCursor.getString(ServerDbAdapter.PASSWORD_COLUMN);
        		
        		Server server = new Server();
        		server.setId(id);
        		server.setName(name);
        		server.setHosturl(url);
        		server.setUsername(username);
        		server.setPassword(password);
        		
        		servers.add(server);
        		Log.i("Name", name);
        		
        	} while (allServersCursor.moveToNext());
        }
        else
        {
        	Log.i("SERVERS", "NO servers found");
        }
		
        this.serveradapter.notifyDataSetChanged();
        
		return true;
	}
	

	public void showServerDialog(final Server server)
	{
		
        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.settings_server_dialog);
        dialog.setTitle(R.string.settings_server_dialog);
        dialog.setCancelable(true);

        Button edit = (Button) dialog.findViewById(R.id.edit);
        Button delete = (Button) dialog.findViewById(R.id.delete);
        
        edit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.hide();
				startEditActivity(server);
			}
		});
        
        delete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ServerDbAdapter dbAdapter = new ServerDbAdapter(getApplicationContext()); 
				dbAdapter.deleteServer(server);
				
				dialog.hide();
			}
		});
        
        // Show the dialog
        dialog.show();
	}
	
	public void startEditActivity(Server server) {

		Intent intent = new Intent();
		
		intent.setClassName("com.joelbitar.switchctrl", "com.joelbitar.switchctrl.serverdetails");
		intent.putExtra(serverdetails.INTENT_EDIT, true);
		
		Log.w("starteditactivity", "Starting the edit activity");
		Log.i("serverid", "" + server.getId());
		
		intent.putExtra(serverdetails.INTENT_EDIT_SERVER_ID, server.getId());
		
		startActivity(intent);
	}
}
